import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { MountBaseEntity } from "../entities/mount-base.entity";

@Injectable()
export class MountBaseService {
  constructor(
    @InjectRepository(MountBaseEntity)
    private readonly mountBaseRepository: Repository<MountBaseEntity>
  ) {}

  /**
   * 获取所有坐骑基础数据列表
   * 用于4423指令响应
   */
  async getAllMounts(): Promise<any[]> {
    try {
      const mounts = await this.mountBaseRepository.find({
        order: { id: "ASC" },
      });

      // 转换为前端需要的格式
      return mounts.map((mount) => ({
        g: mount.price, // 坐骑价格
        hid: mount.id, // 坐骑ID
        id: mount.id, // 基础数据ID
        n: mount.name, // 坐骑名称
        s: 0, // 坐骑状态
        t: mount.type, // 坐骑类型
      }));
    } catch (error) {
      console.error("获取坐骑列表失败:", error);
      throw error;
    }
  }

  /**
   * 根据ID获取坐骑详情
   * 用于4421指令响应
   */
  async getMountById(id: number): Promise<any | null> {
    try {
      const mount = await this.mountBaseRepository.findOne({
        where: { id },
      });

      if (!mount) {
        return null;
      }

      // 转换为前端需要的格式
      return {
        ap: mount.ap,
        apIni: mount.apIni,
        descr: mount.descr,
        hp: mount.hp,
        hpIni: mount.hpIni,
        img: mount.img,
        mp: mount.mp,
        mpIni: mount.mpIni,
        name: mount.name,
        sp: mount.sp,
        spIni: mount.spIni,
        type: mount.type,
      };
    } catch (error) {
      console.error(`获取坐骑详情失败 (ID: ${id}):`, error);
      throw error;
    }
  }

  /**
   * 批量导入坐骑数据
   * 用于初始化数据
   */
  async importMountData(mountData: any[]): Promise<void> {
    try {
      console.log(`开始导入 ${mountData.length} 条坐骑数据...`);

      for (const data of mountData) {
        // 设置坐骑价格：战马免费，其他坐骑1980金币
        const price = data.id === 1 ? 0 : 1980;

        const mount = this.mountBaseRepository.create({
          id: data.id,
          name: data.name,
          descr: data.descr,
          img: data.img,
          ap: data.ap,
          apIni: data.apIni,
          hp: data.hp,
          hpIni: data.hpIni,
          mp: data.mp,
          mpIni: data.mpIni,
          sp: data.sp,
          spIni: data.spIni,
          type: data.type,
          price: price, // 设置价格
        });

        await this.mountBaseRepository.save(mount);
      }

      console.log(`✅ 成功导入 ${mountData.length} 条坐骑数据`);
    } catch (error) {
      console.error("导入坐骑数据失败:", error);
      throw error;
    }
  }

  /**
   * 清空所有坐骑数据
   * 用于重新导入
   */
  async clearAllMounts(): Promise<void> {
    try {
      await this.mountBaseRepository.clear();
      console.log("✅ 已清空所有坐骑数据");
    } catch (error) {
      console.error("清空坐骑数据失败:", error);
      throw error;
    }
  }

  /**
   * 获取坐骑总数
   */
  async getMountCount(): Promise<number> {
    try {
      return await this.mountBaseRepository.count();
    } catch (error) {
      console.error("获取坐骑总数失败:", error);
      throw error;
    }
  }
}
